package cn.kgc.web;

import cn.kgc.dto.AppInfoVo;
import cn.kgc.dto.QueryCondition;
import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.pojo.User;
import cn.kgc.service.AppCategoryService;
import cn.kgc.service.AppInfoService;
import cn.kgc.service.DataDictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.management.Query;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/developer")
public class AppInfoController {
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppInfoService appInfoService;

    @RequestMapping("/appinfolist")
    public String searchAppList(QueryCondition queryCondition,
                                @RequestParam(value = "pageIndex",required = true,defaultValue = "1") Integer pageId,
                                Model model,
                                HttpSession session){
        //app状态列表
        List<DataDictionary> statusList = dataDictionaryService.showDataDictList("APP_STATUS");
        //平台列表
        List<DataDictionary> flatformList = dataDictionaryService.showDataDictList("APP_FLATFORM");
        //查询一级分类
        List<AppCategory> cateList1 = appCategoryService.findCateListByPid(null);
        //取出登录用户
        Integer devId = ((User)session.getAttribute("loginUser")).getId();
        //分页查询app列表
        PageInfo<AppInfoVo> pageInfo = appInfoService.getAppListByPageId(queryCondition, devId, pageId);

        //保存查询结果
        model.addAttribute("statusList",statusList);
        model.addAttribute("flatformList",flatformList);
        model.addAttribute("cateList1",cateList1);
        model.addAttribute("pageInfo",pageInfo);

        //跳转页面
        return "developer/appinfolist";
    }

    @RequestMapping("/toAdd")
    public String toAppInfoAdd(Model model){
        //平台列表
        List<DataDictionary> flatformList = dataDictionaryService.showDataDictList("APP_FLATFORM");
        //查询一级分类
        List<AppCategory> cateList1 = appCategoryService.findCateListByPid(null);

        //保存查询结果
        model.addAttribute("flatformList",flatformList);
        model.addAttribute("cateList1",cateList1);

        return "developer/appinfoadd";
    }

    @RequestMapping("/add")
    public String appInfoAdd(AppInfo appInfo, HttpSession session){
        //取出登录用户
        Integer devId = ((User)session.getAttribute("loginUser")).getId();
        //补充当前登录用户id
        appInfo.setDevId(devId);
        appInfo.setCreatedBy(devId);
        //调用添加app的业务方法
        appInfoService.addAppInfo(appInfo);
        //重新查询app列表
        return "redirect:appinfolist";
    }
}
